package com.gange.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
@Slf4j
@Order(2)
public class CalTimeAspect {
    @Pointcut("execution(* com.gange.service..*.*(..))")
    public void pt(){

    }
    //前置通知
    @Before("pt()")
    public void before(JoinPoint joinPoint){
        log.info("我是前置通知。。。。。");
    }
    @After("pt()")
    public void after(JoinPoint joinPoint){
        log.info("我是后置通知。。。。");
    }
    @Around("pt()")
    public Object calTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        //调用目标方法
        Object proceed = joinPoint.proceed();
        Object target = joinPoint.getTarget();
        log.info("我的目标对象：{}",target);
        Object[] args = joinPoint.getArgs();
        Arrays.stream(args).forEach(System.out::println);
        long endTime = System.currentTimeMillis();
        log.info("{}方法执行了{}毫秒",joinPoint.getSignature().getName(),endTime-startTime);
        return proceed;
    }
}
